#include"RWD.h"
unsigned int port = 3306;
MYSQL mysql;
MYSQL_ROW row;
MYSQL_RES* result2;
bool connection(){
	mysql_init(&mysql);
	
	if (mysql_real_connect(&mysql, host, user, pswd, table, port, NULL, 0)) {
		return true;
	}
	else {
		cout << "connect failed!" << endl;
		return false;
	}
}
void select(int temp) {
	char query[200];
	sprintf_s(query, "select * from cet6 limit %d,%d;", temp, 1);
	mysql_query(&mysql, "set names gbk");
	if (!mysql_query(&mysql, query)) {
		result2 = mysql_store_result(&mysql);
		row = mysql_fetch_row(result2);
	}
	else {
		cout << "failed!!" << endl;
	}
	
}
void RWD(User& U) {
	connection();
	srand((unsigned)time(NULL));
	Element::drawStruct();
	Color::Blue();//设置蓝色
	int i;
	for (i = 0; i < U.getDayWantNumber() - U.getDayCount(); i++) {
		int temp = rand() % 3405;
		select(temp);
		if (_getch() == VK_RETURN) {
			cout << setw(25 + strlen(row[0]) / 2) <<row[0] << endl;
			Element::drawLine();
		}

		if (_getch() == VK_RETURN) {
			cout << setw(25 + strlen(row[1]) / 2) << row[1] << endl;
			Element::drawLine();
		}
		char a = _getch();
		if (a == VK_ESCAPE) {
			break;
		}
		else if (a == VK_RETURN) {
			continue;
		}
		else {
			i--;
			continue;
		}
		if (i >= 20) {
			char s[30] = { "mode con cols = 50 lines = " };
			char d[4];
			_gcvt_s(d, 2*i, 2);
			strcat_s(s, d);
			system(s);
		}
	}
	if (U.getDayCount() <= U.getDayWantNumber()) {
		U.changeDayCount(i);
		U.changeAllCount(i);
	}
	if (U.getDayCount() == U.getDayWantNumber()) {
		Color::Green();//设置绿色
		cout << setw(47) << "恭喜你已完成今天学习计划";
		Sleep(3000);
	}
	mysql_free_result(result2);
	Color::White();
}